﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Air_condition.DO;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Air_condition.DO
{
    [Serializable]
    public class Roof : Construction
    {
        // Attributes of roof object
        private double dbabsorptionCoefficient;
        private double dbthermalRadiation;
        private double dbscoreEffectOfRoofColor;
        private int isurface = -1;
        private int icolor = -1;

        // Public access to attributes
        public double dbAbsorptionCoefficient
        {
            get { return dbabsorptionCoefficient; }
            set { dbabsorptionCoefficient = value; }
        }

        public double dbThermalRadiation
        {
            get { return dbthermalRadiation; }
            set { dbthermalRadiation = value; }
        }

        public int iSurface
        {
            get { return isurface; }
            set { isurface = value; }
        }

        public int iColor
        {
            get { return icolor; }
            set { icolor = value; }
        }

        //  Tính hiệu nhiệt độ dư
        /// <summary>
        /// Calculate temperature different (delta T) 
        /// </summary>
        /// <param name="dbinsideRoomTemp">inside room temperature</param>
        /// <param name="dboutsideRoomTemp">outside room temperature</param>
        public override void CalculateTemperatureDiff(double dbinsideRoomTemp, double dboutsideRoomTemp)
        {
            double dbr = dbthermalRadiation / 0.88;
            double dbtn = dboutsideRoomTemp + ((dbabsorptionCoefficient * dbr) / 20);
            dbtemperatureDiff = dbtn - dbinsideRoomTemp;
        }

        /// <summary>
        /// Calculate heat transfer through roof (Q2) 
        /// </summary>
        /// <param name="dbinsideRoomTemp">inside room temperature</param>
        /// <param name="dboutsideRoomTemp">outside room temperature</param>
        /// <returns>heat transfer through roof (double number)</returns>
        public override double CalculateConstructionHeatTransfer(double dbinsideTemperature, double dboutsideTemperature)
        {
            CalculateTemperatureDiff(dbinsideTemperature, dboutsideTemperature);

            //  Nếu là mái sẫm 
            if (icolor == 0)
            {
                dbscoreEffectOfRoofColor = 1;
            }
            else
            {
                //  Nếu là mái sáng
                if (icolor == 1)
                {
                    dbscoreEffectOfRoofColor = 0.87;
                }
                else
                {
                    //  Nếu là mái màu trung bình
                    if (icolor == 2)
                    {
                        dbscoreEffectOfRoofColor = 0.87;
                    }
                }
            }
            dbarea = dbWidth * dblength;
            return dbarea * dbheatTransferCoefficient * dbtemperatureDiff * dbscoreEffectOfRoofColor;
        }

        /// <summary>
        /// method to copy Roof object to a new object
        /// </summary>
        /// <returns>a new object is Roof object type</returns>
        public object Clone()
        {
            object clone;
            try
            {
                using (MemoryStream stream = new MemoryStream())
                {

                    BinaryFormatter formatter = new BinaryFormatter();

                    // Serialize this object

                    formatter.Serialize(stream, this);

                    stream.Position = 0;

                    // Deserialize to another object

                    clone = formatter.Deserialize(stream);

                }
                return clone;
            }
            catch (Exception e)
            {
                return e;
            }

        }

    }
}
